<?php
/**
 * Description: 以model中指定时间字段为标准，转换新的 utc 标准时间字段
 * Author: hezhizheng
 * Date: 2018/10/15
 * Time: 11:45
 * Created by Created by Panxsoft.
 */

namespace App\Services\Utils;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
 * Trait TransformUtc
 *
 * @property-read string $utc_created_at
 *
 * @package App\Services\Utils
 *
 * @mixin Model
 */
trait UTCTime
{
    /**
     * @used-by $utc_created_at
     *
     * @test
     *
     * @return String
     */
    public function getUtcCreatedAtAttribute(): ?string
    {
        if (! isset($this->attributes['created_at'])) {
            return null;
        }

        return (new Carbon($this->attributes['created_at']))->setTimezone('utc')->toDateTimeString();
    }
}